<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06- 添加和删除属性：$set、$delete</title>
    <script src="../js/vue.min.js"></script>
    <script src="../js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <button @click="doUpdate">修改属性update</button>
    <button @click="doAdd">添加属性doAdd</button>
    <button @click="doDelete">修改属性doDelete</button>

    <hr>
    <h2>{{user.name}}</h2>
    <h2>{{user.age}}</h2>

</div>

<script>
    var vm = new Vue({
        el: '#app',
        data() {
            return {
                user: {
                    id: 1001,
                    name: 'tom'
                }
            }
        },
        methods: {
            doUpdate() {
                this.user.name = 'tomcat'

            },
            doAdd() {
                if (this.user.age) {
                    this.user.age++;
                } else {
                    Vue.set(this.user, 'age', 1)
                }
                console.log(this.user)
            },
            doDelete() {
                if (this.user.age) {
                    //  delete  this.user.age; // 无效
                    Vue.delete(this.user, 'age');
                }

            }
        },
    });

</script>
</body>
</html>